Skip to content

Incrementally update StateCache weight on extend - #41396

Open
stankiewicz wants to merge 3 commits into
apache:masterfrom
stankiewicz:fix-statecache-incremental-extend
Open

stankiewicz wants to merge 3 commits into
apache:masterfrom
stankiewicz:fix-statecache-incremental-extend

Conversation

@stankiewicz

Copy link
Copy Markdown
Contributor

GlobalCachingStateHandler.extend() previously mutated cached list entries in place and re-inserted them via StateCache.put(), which ran objsize.get_deep_size() across the entire accumulated list on every bundle commit (quadratic O(B * N) deep GC referent traversal as a bag grows to N elements across B bundles).

Add StateCache.extend() to measure only the newly appended elements plus any list container resize delta when extending a cached list entry.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR introduces StateCache.extend() and updates GlobalCachingStateHandler.extend() to use it, avoiding repeated full-list objsize.get_deep_size() traversals on every bundle commit when extending cached bag state. The implementation is clean, thread-safe, properly accounts for both the deep size of newly appended elements and the shallow container resize delta, and includes appropriate unit test coverage.

🔍 General Feedback

  • Computing get_deep_size(*elements) outside the cache lock while re-validating the cache entry under the lock matches the concurrency pattern used in get() and put().
  • Measuring individual elements via *elements plus the container's shallow size delta accurately tracks the list's memory footprint in O(delta) time instead of O(N).

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions github-actions Bot added python runners size/s S: 10-49 lines changed labels Nov 16, 2026
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.34%. Comparing base (1f7709a) to head (021860d).
⚠️ Report is 268 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #41396      +/-   ##
============================================
+ Coverage     59.17%   59.34%   +0.17%     
  Complexity     4925     4925              
============================================
  Files          1462     1464       +2     
  Lines        186310   186972     +662     
  Branches       3669     3669              
============================================
+ Hits         110247   110964     +717     
+ Misses        72707    72652      -55     
  Partials       3356     3356              
Flag Coverage Δ
python 80.73% <100.00%> (+0.21%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm for label python.
Backup reviewer: claudevdm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Drop the redundant pre-sizing lock check and duplicate list copy in StateCache.extend, and restrict cached values to list so sys.getsizeof tracks container growth accurately. Keep missing and loading entries as a no-op because another worker thread can evict or reload the key while elements are sized outside the lock.

@damccorm damccorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, one more nit - feel free to address/merge

Co-authored-by: Danny McCormick <dannymccormick@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants